C++20 extends the using
declaration to enum class
. using enum
introduces all the enum
constants
into the current scope.
enum class rgbaColorChannel { red, green, blue, alpha };
std::string_view toString(rgbaColorChannel channel) {
switch (channel) {
using enum rgbaColorChannel;
case red: return "red";
case green: return "green";
case blue: return "blue";
case alpha: return "alpha";
}
}
As with other using
declarations, using enum
improves readability when used in small scopes yet it generates confusion in
large scopes.
The switch
statement, as in the example above, is a natural scope for using enum
.
This rule reports scopes that use a particular enum class
extensively and can benefit from using enum
declaration. For
example, it reports most switch
statements applied to an enum
value.
Exceptions
The rule does not apply if adding the using enum
clause would create a name collision or reduce readability by shadowing a name.